//
//  hoverButton.m
//  demo3
//
//  Created by 张仕儒 on 2021/7/8.
//

#import "hoverButton.h"


@implementation hoverButton

+ (instancetype)initialWithNibName:(NSString *)nibName{
    if (nibName == nil) {
        nibName = @"hoverButton";
    }
    NSNib *nib = [[NSNib alloc] initWithNibNamed:nibName bundle:nil];
    NSArray *objects = nil;
    BOOL isInitial = [nib instantiateWithOwner:self topLevelObjects:&objects];
    if (isInitial) {
        for (id obj in objects) {
            if([obj isKindOfClass:[hoverButton class]]) {
                hoverButton *btn = (hoverButton *)obj;
                return  btn;
            }
        }
    }
    return nil;
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (void)setBoxView:(NSBox *)box{
    _boxView = box;
}

- (void)updateTrackingAreas {
    [super updateTrackingAreas];
    [self ensureTrackingArea];
    if(![[self trackingAreas]containsObject:_trackingArea]) {
        [self addTrackingArea:_trackingArea];
    }
}

- (void)ensureTrackingArea {
    if(_trackingArea == nil) {
        self.trackingArea = [[NSTrackingArea alloc]initWithRect:NSZeroRect options:NSTrackingMouseEnteredAndExited|NSTrackingInVisibleRect owner:self userInfo:nil];
    }
}

- (void)mouseEntered:(NSEvent *)event{
    [self.boxView setHidden:NO];
}

- (void)mouseExited:(NSEvent *)event{
    [self.boxView setHidden:YES];

}

//- (void)mouseEntered:(NSEvent *)event{
//    [self.boxView setHidden: NO];
//}

//- (void)mouseExited:(NSEvent *)event{
//    [self.boxView setHidden: YES];
//
//}


@end
